package com.xxxx.server.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.xxxx.server.listener.EmployeeExcelListener;
import com.xxxx.server.mapper.EmployeeMapper;
import com.xxxx.server.page.RespPageBean;
import com.xxxx.server.pojo.*;
import com.xxxx.server.service.*;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhoubin
 * @since 2020-08-17
 */
@RestController
@RequestMapping("/employee/basic")
@Slf4j
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    @Resource
    private EmployeeMapper employeeMapper;

    @Autowired
    private IPositionService positionService;

    @Autowired
    private INationService nationService;

    @Autowired
    private IJoblevelService joblevelService;

    @Autowired
    private IPoliticsStatusService politicsStatusService;

    @Autowired
    private IDepartmentService departmentService;

    @Resource
    private EmployeeExcelListener employeeExcelListener;


    //进入员工页面 展示员工数据信息
    @GetMapping("/")
    @ApiOperation(value = "查询员工对象")
    public RespPageBean getEmployeeByPage(@RequestParam(defaultValue = "1") Integer currentPage, @RequestParam(defaultValue = "10") Integer size, Employee employee, Date[] beginDateScope) {
        return employeeService.getEmployeeByPage(currentPage, size, employee, beginDateScope);
    }


    //添加员工信息
    @PostMapping("/")
    @ApiOperation(value = "添加员工信息")
    public RespBean addEmployee(@RequestBody EmployeeAdd employeeAdd) {
        //service 层做校验
        if (employeeService.saveEmployee(employeeAdd) == 1) {
            return RespBean.success("添加成功！");
        }
        return RespBean.error("添加失败！！！");
    }


    //更新员工信息
    @PutMapping("/")
    @ApiOperation(value = "更新员工信息")
    public RespBean updateEmployee(@RequestBody Employee employee) {
        if (employeeService.updateEmployeeById(employee) == 1) {
            return RespBean.success("更新成功！！！");
        }
        return RespBean.error("更新失败！！");
    }


    //删除员工信息
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除员工信息")
    public RespBean deleteEmployee(@PathVariable Integer id) {
        if (employeeService.removeEmployeeById(id) == 1) {
            return RespBean.success("删除成功！！！");
        }
        return RespBean.error("删除失败！！");
    }


    @GetMapping("/positions")
    @ApiOperation(value = "查询出所有的职位")
    public List<Position> positions() {
        return positionService.list();
    }

    @GetMapping("/nations")
    @ApiOperation(value = "查询出所有的名族")
    public List<Nation> nations() {
        return nationService.list();
    }

    @GetMapping("/joblevels")
    @ApiOperation(value = "查询出所有的职称")
    public List<Joblevel> joblevels() {
        return joblevelService.list();
    }

    @GetMapping("/politicsstatus")
    @ApiOperation(value = "查询出所有的政治面貌")
    public List<PoliticsStatus> politicsStatuses() {
        return politicsStatusService.list();
    }

    @GetMapping("/deps")
    @ApiOperation(value = "查询出所有的所属部门")
    public List<Department> departments() {
        return departmentService.getAllDepartment();
    }

    @GetMapping("/maxWorkID")
    @ApiOperation(value = "查询出工号")
    public RespBean maxWorkID() {
        RespBean respBean = RespBean.build().setCode(200).setObj(String.format("%08d", employeeService.maxWorkID() + 1));
        return respBean;
    }


    @GetMapping("/export")
    @ApiOperation(value = "导出数据")
    public void exportData(HttpServletResponse response) {
        List<Employee> exports = employeeService.findAllEmployee();
        //List< Employee > exports = (List<Employee>) employeeService.getEmployeeByPage(null,null,new Employee(),null).getData();
        try {
            ServletOutputStream out = response.getOutputStream();
            ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX, true);
            String fileName = new String("Student信息表".getBytes("utf-8"), "utf-8");
            Sheet sheet = new Sheet(1, 0, Employee.class);
            sheet.setSheetName("sheet1");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            response.setContentType("multipart/form-data");
            response.setCharacterEncoding("utf-8");
            writer.write(exports, sheet);
            writer.finish();
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 导入数据
     */
    @PostMapping("/import")
    public RespBean addEmployee(MultipartFile file){
        // 上传过来的excel文件
        try {
            InputStream inputStream = file.getInputStream();

            EasyExcel.read(inputStream,EmployeeImport.class,employeeExcelListener).sheet().doRead();

            return RespBean.success("上传成功");
        } catch (IOException e) {
            e.printStackTrace();
            return RespBean.error("上传失败");
        }
    }

}
